﻿using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WPFExcelReport
{
    /// <summary>
    /// 列举出现有的相关的发货单
    /// 新建发货单
    /// 将销售单行加入现在的发货单
    /// 新建发货单必须选择其中一行，建立VVT和VV
    /// WZ09留位，取消留位，通知WV03
    ///WV03，确定分批，取消分批，取消留位，通知WZ09
    /// </summary>
    public partial class SJHWV04
    {

        #region ==IsWBFull===========
        public static readonly DependencyProperty IsWBFullProperty = DependencyProperty.Register("IsWBFull",
            typeof(bool), typeof(SJHWV04),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsWBFullChanged));
        public bool IsWBFull
        {
            get
            {
                return (bool)GetValue(IsWBFullProperty);
            }
            set
            {
                SetValue(IsWBFullProperty, value);
                OnIsWBFullChg(value);
            }
        }
        private static void OnIsWBFullChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as SJHWV04;
            if (src != null)
            {
                src.IsWBFull = (bool)e.NewValue;
            }
        }
        #endregion
        public void OnIsWBFullChg(bool value)
        {
            if (value)
            {

                ZoneVK.Visibility = Visibility.Collapsed;
                ZoneWB.SetValue(Grid.RowProperty, 1);
                ZoneWB.SetValue(Grid.RowSpanProperty, 2); 
            }
            else
            { 
                ZoneVK.Visibility = Visibility.Visible;
                ZoneWB.SetValue(Grid.RowProperty, 2);
                ZoneWB.SetValue(Grid.RowSpanProperty, 1);
            }
        }

        #region ==CheckCPCW===========
        public static readonly DependencyProperty CheckCPCWProperty = DependencyProperty.Register("CheckCPCW",
            typeof(bool), typeof(SJHWV04),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCheckCPCWChanged));
        public bool CheckCPCW
        {
            get
            {
                return (bool)GetValue(CheckCPCWProperty);
            }
            set
            {
                SetValue(CheckCPCWProperty, value);
                if (value)
                {
                    CheckCP = false;
                    CheckCW = false;
                }
            }
        }
        private static void OnCheckCPCWChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as SJHWV04;
            if (src != null)
            {
                src.CheckCPCW = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==CheckCP===========
        public static readonly DependencyProperty CheckCPProperty = DependencyProperty.Register("CheckCP",
            typeof(bool), typeof(SJHWV04),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCheckCPChanged));
        public bool CheckCP
        {
            get
            {
                return (bool)GetValue(CheckCPProperty);
            }
            set
            {
                SetValue(CheckCPProperty, value);
                if (value)
                {
                    CheckCPK = false;
                    CheckCW = false;
                    CheckCPCW = false;
                }
            }
        }
        private static void OnCheckCPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as SJHWV04;
            if (src != null)
            {
                src.CheckCP = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==CheckCW===========
        public static readonly DependencyProperty CheckCWProperty = DependencyProperty.Register("CheckCW",
            typeof(bool), typeof(SJHWV04),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCheckCWChanged));
        public bool CheckCW
        {
            get
            {
                return (bool)GetValue(CheckCWProperty);
            }
            set
            {
                SetValue(CheckCWProperty, value);
                if (value)
                {
                    CheckCPK = false;
                    CheckCP = false;
                    CheckCPCW = false;
                }
            }
        }
        private static void OnCheckCWChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as SJHWV04;
            if (src != null)
            {
                src.CheckCW = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==CheckCPK===========
        public static readonly DependencyProperty CheckCPKProperty = DependencyProperty.Register("CheckCPK",
            typeof(bool), typeof(SJHWV04),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCheckCPKChanged));
        public bool CheckCPK
        {
            get
            {
                return (bool)GetValue(CheckCPKProperty);
            }
            set
            {
                SetValue(CheckCPKProperty, value);
                if (value)
                {
                    CheckCPCW = false;
                    CheckCP = false;
                    CheckCW = false;
                }
            }
        }
        private static void OnCheckCPKChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as SJHWV04;
            if (src != null)
            {
                src.CheckCPK = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==FART===========
        public static readonly DependencyProperty FARTProperty = DependencyProperty.Register("FART",
        typeof(string), typeof(SJHWV04),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFARTChanged));
        public string FART
        {
            get
            {
                return (string)GetValue(FARTProperty);
            }
            set
            {
                SetValue(FARTProperty, value);
            }
        }
        private static void OnFARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as SJHWV04;
            if (src != null)
            {
                src.FART = (string)e.NewValue;
            }
        }
        #endregion 
        
        #region ==VVT===========
        public static readonly DependencyProperty VVTProperty = DependencyProperty.Register("VVT",
        typeof(string), typeof(SJHWV04),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVVTChanged));
        public string VVT
        {
            get
            {
                return (string)GetValue(VVTProperty);
            }
            set
            {
                SetValue(VVTProperty, value);
            }
        }
        private static void OnVVTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as SJHWV04;
            if (src != null)
            {
                src.VVT = (string)e.NewValue;
            }
        }
        #endregion 

        ITXEXRPT ITXEXRPT;
        private ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        } 
        public SJHWV04()
        { 
            
            DataContext = new VSJHWV04();
            InitializeComponent();
            this.Name = "发货组织";
            if (ITXEXRPT == null)
            {
                ITXEXRPT = new ITXEXRPT();
                ITXEXRPT.Root = "C:\\IntexReport";
                ITXEXRPT.strCompany = app.company;
                ITXEXRPT.PrintType = "OpenExcel";
            } 
             cVK05.IsNVV = true;
             cWZ09.LO = "01";
             cWZ09.LB = "CP";
             cVK05.ARTST = "F";
             cVK04.LFD = "000014";
             cVK05.LFD = "000014";
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
        } 
        VWBBST wb;
        private void btnPrintPK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                
                if(FART.ToString().Trim().Length<2)
                {
                    MessageBox.Show("请先查询订单！");
                    return;
                }
                if (VVT.ToString().Trim().Length < 20)
                {
                    MessageBox.Show("请先选择发货单号！");
                    return;
                }
                string KRS = "", LFD = "", LIEFSCH = "";
                KRS = VVT.ToString().Trim().Substring(1, 2);
                LFD = VVT.ToString().Trim().Substring(3, 6);
                LIEFSCH = VVT.ToString().Trim().Substring(12, 8);
                ITXEXRPT.dsSource = new DataSet();
                DataSet SResult = new DataSet();
                if (FART.ToString().Trim() == "NA" || FART.ToString().Trim() == "N")
                {
                    ITXEXRPT.DocType = "TSTVV05";
                    DataRow dwData = VVVLIEF.Instance.TBMain.NewRow();
                    dwData["VVMANDANT"] = app.Mandant;
                    dwData["VVPAUFKRS"] = KRS;
                    dwData["VVPAUFLFD"] = LFD;
                    dwData["VVLIEFSCH"] = LIEFSCH;
                    SResult = VVVLIEF.Instance.QueryVVLIEF(dwData, 10);
                    ITXEXRPT.dsSource.Merge(SResult); 
                    dwData = VVVTLIEF.Instance.TBMain.NewRow();
                    dwData["VVTMANDANT"] = app.Mandant;
                    dwData["VVTAUFKRS"] = KRS;
                    dwData["VVTAUFLFD"] = LFD;
                    dwData["VVTLIEFSCH"] = LIEFSCH;
                    SResult = VVVTLIEF.Instance.QueryVVTLIEF(dwData, 15);
                    ITXEXRPT.dsSource.Merge(SResult);
                    if (wb == null) wb = new VWBBST();
                    dwData = wb.TBMain.NewRow();
                    dwData["WBBMANDANT"] = app.Mandant;
                    dwData["WBBALIEFSCH"] = LIEFSCH;
                    SResult = wb.QueryWBBST(dwData, 20);
                    ITXEXRPT.dsSource.Merge(SResult);
                    ITXEXRPT.strExcelFile = @"\TST\TSTVV05.xls";
                    string filename = ITXEXRPT.WriteExcelReport();
                }
                else
                {                    
                    ITXEXRPT.DocType = "TSTVVSMP";
                    DataRow dwData = VVVLIEF.Instance.TBMain.NewRow();
                    dwData["VVMANDANT"] = app.Mandant;
                    dwData["VVPAUFKRS"] = KRS;
                    dwData["VVPAUFLFD"] = LFD;
                    dwData["VVLIEFSCH"] = LIEFSCH;
                    SResult = VVVLIEF.Instance.QueryVVLIEF(dwData, 10);
                    ITXEXRPT.dsSource.Merge(SResult);
                    string TAX = SResult.Tables["VVLIEF"].Rows[0]["ACCOUNT"].ToString().Trim();
                    dwData = VVVTLIEF.Instance.TBMain.NewRow();
                    dwData["VVTMANDANT"] = app.Mandant;
                    dwData["VVTAUFKRS"] = KRS;
                    dwData["VVTAUFLFD"] = LFD;
                    dwData["VVTLIEFSCH"] = LIEFSCH;
                    SResult=VVVTLIEF.Instance.QueryVVTLIEF(dwData, 14);
                    ITXEXRPT.dsSource.Merge(SResult);
                    dwData = VLIEFKO.Instance.TBMain.NewRow();
                    dwData["LSKMANDANT"] = app.Mandant;
                    dwData["LSKLIEFNR"] = " AND LSKLIEFNR='00000000'";
                    dwData["LSKKONDSL"] = " AND TRIM(LSKKONDSL)='" + TAX + "'";
                    SResult=VLIEFKO.Instance.QueryLIEFKO(dwData, 12);   
                    ITXEXRPT.dsSource.Merge(SResult);
                    ITXEXRPT.strExcelFile = @"\TST\TSTVVSMP.xls";
                    string filename = ITXEXRPT.WriteExcelReport();
                }

                /*
                if (txtLang.Text == "EN")
                {
                    ITXEXRPT.DocType = "JLTVV03PKEN";
                }
                
                ITXEXRPT.dsSource.Merge(cVVT.MV.SetProd);
                ITXEXRPT.dsSource.Merge(this.cVK05.MV.SetProd);
                if (wb == null) wb = new VWBBST();
                DataRow dwData = wb.TBMain.NewRow();
                dwData["WBBMANDANT"] = app.Mandant;
                dwData["WBBAUFKRS"] = cWZ09.KRS;
                dwData["WBBAUFLFD"] = cWZ09.LFD;
                dwData["WBBAUFPOS"] = cWZ09.POS;
                dwData["WBBALIEFSCH"] = cWZ09.LIEFSCH;

                DataSet SResult = wb.QueryWBBST(dwData, 17);
                string filter = "";
                DataRow[] rows = null;
                DataTable tb;
                if (CheckCW)
                {
                    filter = "WBBLB='CW'";
                    rows = SResult.Tables[0].Select(filter);
                    if (rows.Length == 0)
                        return;
                    tb = rows.CopyToDataTable();
                    tb.TableName = "WBBST";

                    ITXEXRPT.dsSource.Merge(tb);
                    SResult = ((VVKPOS)cVK05.MV).QueryVKP(cVK05.dwVKP);
                    ITXEXRPT.dsSource.Merge(SResult);
                    ITXEXRPT.strExcelFile = @"\JLT\JLTVVDOC.xls";
                    string filename = ITXEXRPT.WriteExcelReport();
                    return;
                }*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

         
        
   
    }
}
